﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;


namespace CNVP.JShop.Web.Admin
{
    public partial class OrderAdd : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 97)) return;//判断页面权限
            if (!IsPostBack)
            {
                DataInfo();
            }
        }

        private void DataInfo()
        {
            string html = "";
            #region 支付方式
            html = "";
            var ColumnPayWay = new BLL.CNVP_Column().GetAll_CNVP_Column(" ColumnType in (5,7) and IsShow=1");
            if (ColumnPayWay != null && ColumnPayWay.Count > 0)
            {
                html += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"  widtd=\"100%\" class=\"division\">";
                for (int i = 0; i < ColumnPayWay.Count; i++)
                {
                    string temp = "";
                    if (i == 0)
                    {
                        temp = "checked=\"checked\"";
                    }
                    html += "		<tr >";
                    html += "			<th style=\"text-align:left;\">";
                    html += "				<input id=\"PayWay" + ColumnPayWay[i].ID + "\" type=\"radio\" value=\"" + ColumnPayWay[i].ID + "\" name=\"PayWay\" " + temp + "  />" + ColumnPayWay[i].ColumnName + "";
                    html += "			</th>";
                    html += "			<td>";
                    html += "				" + ColumnPayWay[i].ColumnName + "";
                    html += "			</td>";
                    html += "		</tr>";
                }
                html += "</table>";
            }
            lbPayWay.Text = html;
            #endregion
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                string LengthName_Str = "";
                int SaleNum_Int = 0;
                int proDetailID_Int = 0;
                int productID_Int = 0;
                string _LengthName = Request.Form["LengthName"];
                string _ImgProLoad = Request.Form["ImgProLoad"];
                string _ProductClassID = Request.Form["ProductClassID"];
                string _ProDetailNo = Request.Form["ProDetailNo"];
                string _SalePrice = Request.Form["SalePrice"];
                string _SaleNum = Request.Form["SaleNum"];
                string _proID = Request.Form["proID"];
                string _proDetailID = Request.Form["proDetailID"];
                #region 必须先判断库存
                string[] txtHotLinkArray = txtHotLink.Text.Split(Convert.ToChar(","));
                for (int i = 0; i < txtHotLinkArray.Length; i++)
                {
                    string[] LengthNameValue = _LengthName.Split(Convert.ToChar(","));
                    LengthName_Str = LengthNameValue[i];
                    string[] SaleNumValue = _SaleNum.Split(Convert.ToChar(","));
                    try
                    {
                        SaleNum_Int = Convert.ToInt32(SaleNumValue[i]);
                    }
                    catch { SaleNum_Int = 0; }
                    string[] proIDValue = _proID.Split(Convert.ToChar(","));//产品主表ID
                    productID_Int = Convert.ToInt32(proIDValue[i]);
                    string[] proDetailIDValue = _proDetailID.Split(Convert.ToChar(","));//详细表ID
                    proDetailID_Int = Convert.ToInt32(proDetailIDValue[i]);
                    if (proDetailID_Int != 0)
                    {
                        var model_ProductsDetail = new BLL.CNVP_ProductsDetail().GetSingle_CNVP_ProductsDetailById(proDetailID_Int);
                        if (model_ProductsDetail != null)
                        {
                            if (SaleNum_Int > model_ProductsDetail.StockNum)
                            {
                                BasePage.MessageBoxGoHistory("" + LengthName_Str + "  库存量不足！");
                                return;
                            }
                        }
                    }
                    else
                    {
                        var model_Products = new BLL.CNVP_Products().GetSingle_CNVP_ProductsById(productID_Int);
                        if (model_Products != null)
                        {
                            if (SaleNum_Int > model_Products.StockTotalNum)
                            {
                                BasePage.MessageBoxGoHistory("" + LengthName_Str + "  库存量不足！");
                                return;
                            }
                        }
                    }

                }
                #endregion

                string DeliveryWay = "";
                decimal DeliveryPrice = 0;
                string _DeliveryWayPrice = Request.Form["DeliveryWayPrice"];
                if (!string.IsNullOrEmpty(_DeliveryWayPrice))
                {
                    string[] arrWayPrice = _DeliveryWayPrice.Split(Convert.ToChar(":"));
                    if (arrWayPrice.Length > 0)
                    {
                        DeliveryWay = arrWayPrice[0];
                        DeliveryPrice = Convert.ToDecimal(arrWayPrice[1]);
                    }
                }
                int rows = new BLL.CNVP_Order().Add_CNVP_Order(new Model.CNVP_Order()
                {
                    BuyerID = int.Parse(txtUserID.Text),
                    AddTime = DateTime.Now,
                    PostTime = DateTime.Now,
                    OrderNum = DateTime.Now.ToString("yyyyMMddhhMMss"),
                    Province = Request.Form["ListProvince"],
                    City = Request.Form["ListCity"],
                    Area = Request.Form["ListArea"],
                    GetAddress = Request.Form["txtGetAddress"],
                    GetTrueName = Request.Form["txtTrueName"],

                    MobilePhone = Request.Form["txtMobilePhone"],
                    Phone = Request.Form["txtPhone"],
                    ZipCode = Request.Form["txtZipCode"],
                    DeliveryWay = DeliveryWay,
                    DeliveryPrice = DeliveryPrice,
                    PayWay = Request.Form["PayWay"],
                    ProductsTotalAmount = decimal.Parse(Request.Form["inputSalePriceTatol"]),
                    OrderTotalAmount = decimal.Parse(Request.Form["inputOrderTatol"]),
                    IsNeedBill = Request.Form["GetInvoice"] == "1" ? Convert.ToByte(1) : Convert.ToByte(0),
                    OrderState = Convert.ToInt16(Request.Form["PayWay"]) == (byte)103 ? (byte)1 : (byte)0, //货到付款‘103’还是在线支付
                    Text = txtText.Text.Trim()
                });

                #region 添加订单产品详细表
                string ImgProLoad_Str = "";
                int ProductClassID_Int = 0;
                string ProDetailNo_Str = "";
                decimal SalePrice_decimal = 0;
                decimal TotalPrice_decimal = 0;
                for (int i = 0; i < txtHotLinkArray.Length; i++)
                {
                    string[] LengthNameValue = _LengthName.Split(Convert.ToChar(","));
                    LengthName_Str = LengthNameValue[i];
                    string[] ImgProLoadValue = _ImgProLoad.Split(Convert.ToChar(","));
                    ImgProLoad_Str = ImgProLoadValue[i];
                    string[] ProductClassIDValue = _ProductClassID.Split(Convert.ToChar(","));
                    ProductClassID_Int = Convert.ToInt32(ProductClassIDValue[i]);
                    string[] ProDetailNoValue = _ProDetailNo.Split(Convert.ToChar(","));
                    ProDetailNo_Str = ProDetailNoValue[i];
                    string[] SalePriceValue = _SalePrice.Split(Convert.ToChar(","));
                    SalePrice_decimal = Convert.ToDecimal(SalePriceValue[i]);
                    string[] SaleNumValue = _SaleNum.Split(Convert.ToChar(","));
                    try
                    {
                        SaleNum_Int = Convert.ToInt32(SaleNumValue[i]);
                    }
                    catch { SaleNum_Int = 0; }
                    TotalPrice_decimal = SalePrice_decimal * SaleNum_Int;//计算总额
                    string[] proIDValue = _proID.Split(Convert.ToChar(","));//产品主表ID
                    productID_Int = Convert.ToInt32(proIDValue[i]);
                    string[] proDetailIDValue = _proDetailID.Split(Convert.ToChar(","));//详细表ID
                    proDetailID_Int = Convert.ToInt32(proDetailIDValue[i]);
                    int rows_OrderProducts = new BLL.CNVP_OrderProducts().Add_CNVP_OrderProducts(new Model.CNVP_OrderProducts()
                    {
                        ProductName = LengthName_Str,
                        ImgProLoad = ImgProLoad_Str,
                        ProductClassID=ProductClassID_Int,
                        ProductID = productID_Int,
                        ProductsDetailID = proDetailID_Int,
                        ProductNo = ProDetailNo_Str,
                        Price = SalePrice_decimal,
                        Num = SaleNum_Int,
                        TotalPrice = TotalPrice_decimal,
                        OrderID = rows
                    });
                }
                #endregion

                #region 购物必填信息
                try
                {
                    int ProID_Int = 0;
                    int ProDetailID_Int = 0;
                    string _ProID_ProDetailIDS = Request.Form["ProID_ProDetailIDS"];
                    if (!string.IsNullOrEmpty(_ProID_ProDetailIDS))
                    {
                        string[] ProID_ProDetailIDSArray = _ProID_ProDetailIDS.Split(Convert.ToChar(","));
                        for (int k = 0; k < ProID_ProDetailIDSArray.Length; k++)
                        {
                            string[] IDSArray = ProID_ProDetailIDSArray[k].Split(Convert.ToChar("_"));
                            ProID_Int = Convert.ToInt32(IDSArray[0]);//主产品ID
                            ProDetailID_Int = int.Parse(IDSArray[1]);//产品详细ID

                            int ProTypeRefShopMustInfoID_Int = Convert.ToInt32(Request.Form["CKRefShopMustInfo" + ProID_ProDetailIDSArray[k] + ""]);
                            string _MustInfoValue = Request.Form["MustInfoValue" + ProID_ProDetailIDSArray[k] + ""];
                            if (!string.IsNullOrEmpty(_MustInfoValue))
                            {
                                int rows_OrderProShopMustInfo = new BLL.CNVP_OrderProShopMustInfo().Add_CNVP_OrderProShopMustInfo(new Model.CNVP_OrderProShopMustInfo()
                                {
                                    ProductID = ProID_Int,
                                    ProDetailsID = ProDetailID_Int,
                                    OrderID = rows,
                                    ProTypeRefShopMustInfoID = ProTypeRefShopMustInfoID_Int,
                                    StandardValue = _MustInfoValue
                                });
                            }
                        }
                    }
                }
                catch
                { }
                #endregion

                #region 编辑，保存购物地址
                if (Request.Form["EditGetAddressID"] != null)
                {
                    Model.CNVP_GetAddress model_GetAddress = new Model.CNVP_GetAddress();
                    model_GetAddress.ID = int.Parse(Request.Form["EditGetAddressID"]);
                    model_GetAddress.Province = Request.Form["ListProvince"];
                    model_GetAddress.City = Request.Form["ListCity"];
                    model_GetAddress.Area = Request.Form["ListArea"];
                    model_GetAddress.GetAddress = Request.Form["txtGetAddress"];
                    model_GetAddress.TrueName = Request.Form["txtTrueName"];
                    model_GetAddress.MobilePhone = Request.Form["txtMobilePhone"];
                    model_GetAddress.Phone = Request.Form["txtPhone"];
                    model_GetAddress.ZipCode = Request.Form["txtZipCode"];
                    model_GetAddress.UsersID = int.Parse(txtUserID.Text);
                    new BLL.CNVP_GetAddress().Update_CNVP_GetAddressWithout(model_GetAddress);
                }
                if (Request.Form["cbIsSave"] == "on")
                {
                    int rows_GetAddress = new BLL.CNVP_GetAddress().Add_CNVP_GetAddress(new Model.CNVP_GetAddress()
                    {
                        Province = Request.Form["ListProvince"],
                        City = Request.Form["ListCity"],
                        Area = Request.Form["ListArea"],
                        GetAddress = Request.Form["txtGetAddress"],
                        TrueName = Request.Form["txtTrueName"],
                        MobilePhone = Request.Form["txtMobilePhone"],
                        Phone = Request.Form["txtPhone"],
                        ZipCode = Request.Form["txtZipCode"],
                        IsTolerant = 0,
                        UsersID = int.Parse(txtUserID.Text)
                    });
                }

                #endregion

                txtHotLink.Text = "";
                BasePage.MessageBox("添加成功！", "OrderList.aspx");
            }
            catch
            {
                MessageUtils.ShowPre("悲剧，出错了！");
            }
        }
    }
}